package com.tubi.test;

import com.tubi.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        //创建集合容器
        ArrayList<Student> list = new ArrayList<>();

        lo:
        while (true) {
            //1 .搭建主界面菜单
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");

            String choice = sc.next();

            //使用switch语句
            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent(list);//添加学生方法
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent(list);
                    break;
                case "4":
                    //System.out.println("查看学生");
                    queryStudents(list);//查看学生
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }

    }
    //修改学生
    private static void updateStudent(ArrayList<Student> list) {
        if(list.size()==1){
            System.out.println("请输入学生的学号进行修改");
            Scanner sc = new Scanner(System.in);
            String sid;
            int i;
            while (true){
                System.out.println("请输入要修改的学号");
                sid = sc.next();
                i = getIndex(list,sid);
                if(i==-1){
                    System.out.println("输入的学号不存在");
                }else {
                    break;
                }
            }

            System.out.println("请输入姓名:");
            String name =sc.next();
            System.out.println("请输入年龄:");
            int age =sc.nextInt();
            System.out.println("请输入生日:");
            String birtaday =sc.next();

            Student newS = new Student(sid,name,age,birtaday);
            list.set(i, newS);//根据索引进行修改
            System.out.println("修改成功");
        }else {
            System.out.println("暂时没有信息");
            return;
        }
    }

    //删除信息
    private static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学号");
        String sid = sc.next();
        int i = getIndex(list,sid);
        if(i==-1){
            System.out.println("输入的学号不存在");
            return;
        }else {
            list.remove(i);
            System.out.println("删除成功");
            return;
        }
    }

    //查看学生
    private static void queryStudents(ArrayList<Student> list) {
        if(list.size()<1){
            System.out.println("暂时没有学生信息，请录入");
        }else{
            // 2. 存在: 展示表头数据
            System.out.println("学号\t姓名\t年龄\t生日");
            // 3. 遍历集合, 获取每一个学生对象的信息, 打印在控制台
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                System.out.println(stu.getSid() + "\t\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }

        }
    }

    //添加学生方法
    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        // // 1. 给出录入的提示信息
        String sid;
        while (true){
            System.out.println("请输入学号:");
            sid =sc.next();
            int i =  getIndex(list,sid);
            if(i== -1){
                break; //不存在跳出
            }else {
                System.out.println("学号存在，请重新输入");
            }
        }

        System.out.println("请输入姓名:");
        String name =sc.next();
        System.out.println("请输入年龄:");
        int age =sc.nextInt();
        System.out.println("请输入生日:");
        String birtaday =sc.next();
        //键盘录入的信息封装对象
        Student student = new Student(sid, name, age, birtaday);
        list.add(student);// 添加对象
        System.out.println("添加成功");


    }
    //查看学好存在不存在
    public static int getIndex(ArrayList<Student> list, String sid){
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student st = list.get(i);
            String id = st.getSid();
            if(id.equals(sid)){
                index = i;
            }
        }

        return index;
    }
}
